package org.greyhawk.samonitor.domain;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;
import org.greyhawk.samonitor.domain.Site;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

privileged aspect SiteDataOnDemand_Roo_DataOnDemand {
    
    declare @type: SiteDataOnDemand: @Component;
    
    private Random SiteDataOnDemand.rnd = new SecureRandom();
    
    private List<Site> SiteDataOnDemand.data;
    
    public Site SiteDataOnDemand.getNewTransientSite(int index) {
        org.greyhawk.samonitor.domain.Site obj = new org.greyhawk.samonitor.domain.Site();
        obj.setName("name_" + index);
        obj.setUrlPath("urlPath_" + index);
        return obj;
    }
    
    public Site SiteDataOnDemand.getSpecificSite(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size()-1)) index = data.size() - 1;
        Site obj = data.get(index);
        return Site.findSite(obj.getId());
    }
    
    public Site SiteDataOnDemand.getRandomSite() {
        init();
        Site obj = data.get(rnd.nextInt(data.size()));
        return Site.findSite(obj.getId());
    }
    
    public boolean SiteDataOnDemand.modifySite(Site obj) {
        return false;
    }
    
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void SiteDataOnDemand.init() {
        if (data != null) {
            return;
        }
        
        data = org.greyhawk.samonitor.domain.Site.findSiteEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Site' illegally returned null");
        if (data.size() > 0) {
            return;
        }
        
        data = new java.util.ArrayList<org.greyhawk.samonitor.domain.Site>();
        for (int i = 0; i < 10; i++) {
            org.greyhawk.samonitor.domain.Site obj = getNewTransientSite(i);
            obj.persist();
            data.add(obj);
        }
    }
    
}
